clean_coordinates_debug <- function (x, lon = "decimallongitude", lat = "decimallatitude", 
         species = "species", countries = NULL, tests = c("capitals", 
                                                          "centroids", "equal", "gbif", "institutions", 
                                                          "outliers", "seas", "zeros"), capitals_rad = 10000, 
         centroids_rad = 1000, centroids_detail = "both", inst_rad = 100, 
         outliers_method = "quantile", outliers_mtp = 5, outliers_td = 1000, 
         outliers_size = 7, range_rad = 0, zeros_rad = 0.5, capitals_ref = NULL, 
         centroids_ref = NULL, country_ref = NULL, country_refcol = "iso_a3_eh", 
         inst_ref = NULL, range_ref = NULL, seas_ref = NULL, seas_scale = 50, 
         urban_ref = NULL, value = "spatialvalid", verbose = TRUE, 
         report = FALSE, reportadd = getwd()) 
{
  match.arg(value, choices = c("spatialvalid", "flagged", 
                               "clean"))
  match.arg(centroids_detail, choices = c("both", "country", 
                                          "provinces"))
  match.arg(outliers_method, choices = c("distance", 
                                         "quantile", "mad"))
  nams <- c(lon, lat, species, countries)
  if (!all(nams %in% names(x))) {
    stop(sprintf("%s column not found\n", nams[which(!nams %in% 
                                                       names(x))]))
  }
  if (is.null(countries) & "countries" %in% tests) {
    stop("provide countries column or remove countries test")
  }
  if (is.null(species)) {
    if ("outliers" %in% tests) {
      stop("provide species column or remove outliers test")
    }
    if ("duplicates" %in% tests) {
      stop("provide species column or remove duplicates test")
    }
  }
  out <- data.frame(matrix(NA, nrow = nrow(x), ncol = 13))
  colnames(out) <- c("val", "equ", "zer", 
                     "cap", "cen", "sea", "urb", "con", 
                     "otl", "gbf", "inst", "rang", 
                     "dpl")
  out$val <- cc_val(x, lon = lon, lat = lat, verbose = verbose, 
                    value = "flagged")
  if (!all(out$val)) {
    stop("invalid coordinates found in rows, clean dataset before proceeding:\n", 
         paste(which(!out$val), "\n"))
  }
  if ("equal" %in% tests) {
    out$equ <- cc_equ(x, lon = lon, lat = lat, verbose = verbose, 
                      value = "flagged", test = "absolute")
  }
  if ("zeros" %in% tests) {
    out$zer <- cc_zero(x, lon = lon, lat = lat, buffer = zeros_rad, 
                       verbose = verbose, value = "flagged")
  }
  if ("capitals" %in% tests) {
    out$cap <- cc_cap(x, lon = lon, lat = lat, buffer = capitals_rad, 
                      ref = capitals_ref, value = "flagged", verbose = verbose)
  }
  if ("centroids" %in% tests) {
    out$cen <- cc_cen(x, lon = lon, lat = lat, buffer = centroids_rad, 
                      test = centroids_detail, ref = centroids_ref, value = "flagged", 
                      verbose = verbose)
  }
  if ("seas" %in% tests) {
    out$sea <- cc_sea(x, lon = lon, lat = lat, ref = seas_ref, 
                      scale = seas_scale, verbose = verbose, value = "flagged")
  }
  if ("urban" %in% tests) {
    out$urb <- cc_urb(x, lon = lon, lat = lat, ref = urban_ref, 
                      verbose = verbose, value = "flagged")
  }
  if ("countries" %in% tests) {
    out$con <- cc_coun(x, lon = lon, lat = lat, iso3 = countries, 
                       ref = country_ref, ref_col = country_refcol, verbose = verbose, 
                       value = "flagged")
  }
  if ("outliers" %in% tests) {
    otl_test <- table(x[species])
    otl_test <- otl_test[otl_test > outliers_size]
    otl_test <- x[x[[species]] %in% names(otl_test), ]
    otl_test <- otl_test[, c(species, lon, lat)]
    otl_flag <- cc_outl(otl_test, lon = lon, lat = lat, species = species, 
                        method = outliers_method, mltpl = outliers_mtp, tdi = outliers_td, 
                        value = "ids", verbose = verbose)
    otl <- rep(TRUE, nrow(x))
    otl[as.numeric(otl_flag)] <- FALSE
    out$otl <- otl
  }
  if ("gbif" %in% tests) {
    out$gbf <- cc_gbif(x, lon = lon, lat = lat, verbose = verbose, 
                       value = "flagged")
  }
  if ("institutions" %in% tests) {
    out$inst <- cc_inst(x, lon = lon, lat = lat, ref = inst_ref, 
                        buffer = inst_rad, verbose = verbose, value = "flagged")
  }
  if ("range" %in% tests) {
    if (!is.null(range_rad)) {
      stop("'range_rad' not found")
    }
    else {
      out$rang <- cc_iucn(x, lon = lon, lat = lat, species = species, 
                          buffer = range_rad, verbose = verbose, value = "flagged")
    }
  }
  if ("duplicates" %in% tests) {
    out$dpl <- cc_dupl(x, lon = lon, lat = lat, species = species, 
                       value = "flagged")
  }
  out <- Filter(function(x) !all(is.na(x)), out)
  suma <- as.vector(Reduce("&", out))
  if (verbose) {
    if (!is.null(suma)) {
      message(sprintf("Flagged %s of %s records, EQ = %s.", 
                      sum(!suma, na.rm = TRUE), length(suma), round(sum(!suma, 
                                                                        na.rm = TRUE)/length(suma), 2)))
    }
    else {
      message("flagged 0 records, EQ = 0")
    }
  }
  if (value == "spatialvalid") {
    ret <- data.frame(x, out, summary = suma)
    names(ret) <- c(names(x), paste(".", names(out), 
                                    sep = ""), ".summary")
    class(ret) <- c("spatialvalid", "data.frame", 
                    class(out))
    #out <- ret
    if (report) {
      #report <- "clean_coordinates_report.txt"
    #}
    #if (is.character(report)) {
      repo <- data.frame(Test = as.character(names(out)), 
                         Flagged.records = colSums(!out), stringsAsFactors = FALSE)
      repo <- rbind(repo, c("Total number of records", 
                            length(ret$.summary)))
      repo <- rbind(repo, c("Error Quotient", round(sum(!ret$.summary, 
                                                        na.rm = TRUE)/length(ret$.summary), 2)))
      write.table(repo, reportadd, sep = "\t", row.names = FALSE, 
                  quote = FALSE)
    }
    out <- ret
  }
  if (value == "clean") {
    out <- x[suma, ]
    if (report | is.character(report)) {
      warning("report only valid with value = 'spatialvalid'")
    }
  }
  if (value == "flagged") {
    out <- suma
    if (report | is.character(report)) {
      warning("report only valid with value = 'spatialvalid'")
    }
  }
  return(out)
}
